#!/bin/bash
# $Id$

echo "SemaQTO revision $Revision$ launched"
echo "(Scriptable Enterprise edition Multipurpose test Automation In Progress)"
echo "Licensed under Apache License 2.0, more info: http://code.google.com/p/semaqto/"
echo

if [ ! -r semaqto.conf ]
then
  echo Cannot read configuration file semaqto.conf
  exit 1
fi

source semaqto.conf

if [ "$CONFIG_DIRECTORY" == "" ]
then
  echo "Configuration parameter CONFIG_DIRECTORY not set"
  exit 1
fi

if [ ! -d $CONFIG_DIRECTORY ]
then
  echo Cannot find directory: $CONFIG_DIRECTORY
  exit 1
fi

if [ "$CONFIG_TESTSDIRECTORY" == "" ]
then
  echo "Configuration parameter CONFIG_TESTSDIRECTORY not set"
  exit 1
fi

if [ ! -d $CONFIG_TESTSDIRECTORY ]
then
  echo Cannot find directory: $CONFIG_TESTSDIRECTORY
  exit 1
fi

if [ "$CONFIG_BINARY" == "" ]
then
  echo "Configuration parameter CONFIG_BINARY not set"
  exit 1
fi

if [ ! -x $CONFIG_DIRECTORY/$CONFIG_BINARY ]
then
  echo Cannot execute program to be tested: $CONFIG_DIRECTORY/$CONFIG_BINARY
  exit 1
fi

OUTPUT=`mktemp`

pass=0
fail=0

pushd $CONFIG_DIRECTORY

for TESTFILE in $CONFIG_TESTSDIRECTORY/*.test
do
  TESTNAME=${TESTFILE/.test/}
  echo -n Running test $TESTNAME ...
  ./$CONFIG_BINARY < $TESTFILE > $OUTPUT
  cmp -s $TESTNAME.out $OUTPUT
  if [ $? -eq 0 ]
  then
    echo " PASS"
    let "pass++"
  else
    echo " FAIL"
    let "fail++"
  fi
done

rm $OUTPUT

total=$(($pass + $fail))

echo
echo Total tests: $total. Passed: $pass. Failed: $fail.

popd

exit 0
